/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package resourceplan.impl;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;

import resourceplan.Allocation;
import resourceplan.Person;
import resourceplan.ResourceplanPackage;
import resourceplan.Role;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Allocation</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link resourceplan.impl.AllocationImpl#getRole <em>Role</em>}</li>
 *   <li>{@link resourceplan.impl.AllocationImpl#getPerson <em>Person</em>}</li>
 *   <li>{@link resourceplan.impl.AllocationImpl#getRoleWorkloadRatio <em>Role Workload Ratio</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class AllocationImpl extends EObjectImpl implements Allocation {
	/**
	 * The cached value of the '{@link #getRole() <em>Role</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getRole()
	 * @generated
	 * @ordered
	 */
	protected Role role;

	/**
	 * The cached value of the '{@link #getPerson() <em>Person</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getPerson()
	 * @generated
	 * @ordered
	 */
	protected Person person;

	/**
	 * The default value of the '{@link #getRoleWorkloadRatio() <em>Role Workload Ratio</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getRoleWorkloadRatio()
	 * @generated
	 * @ordered
	 */
	protected static final float ROLE_WORKLOAD_RATIO_EDEFAULT = 0.0F;

	/**
	 * The cached value of the '{@link #getRoleWorkloadRatio() <em>Role Workload Ratio</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getRoleWorkloadRatio()
	 * @generated
	 * @ordered
	 */
	protected float roleWorkloadRatio = ROLE_WORKLOAD_RATIO_EDEFAULT;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected AllocationImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return ResourceplanPackage.Literals.ALLOCATION;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Role getRole() {
		if (role != null && role.eIsProxy()) {
			InternalEObject oldRole = (InternalEObject)role;
			role = (Role)eResolveProxy(oldRole);
			if (role != oldRole) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, ResourceplanPackage.ALLOCATION__ROLE, oldRole, role));
			}
		}
		return role;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Role basicGetRole() {
		return role;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetRole(Role newRole, NotificationChain msgs) {
		Role oldRole = role;
		role = newRole;
		if(person != null)
			person.calcActualWorkload();
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, ResourceplanPackage.ALLOCATION__ROLE, oldRole, newRole);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setRole(Role newRole) {
		if (newRole != role) {
			NotificationChain msgs = null;
			if (role != null)
				msgs = ((InternalEObject)role).eInverseRemove(this, ResourceplanPackage.ROLE__ALLOCATIONS, Role.class, msgs);
			if (newRole != null)
				msgs = ((InternalEObject)newRole).eInverseAdd(this, ResourceplanPackage.ROLE__ALLOCATIONS, Role.class, msgs);
			msgs = basicSetRole(newRole, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ResourceplanPackage.ALLOCATION__ROLE, newRole, newRole));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Person getPerson() {
		if (person != null && person.eIsProxy()) {
			InternalEObject oldPerson = (InternalEObject)person;
			person = (Person)eResolveProxy(oldPerson);
			if (person != oldPerson) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, ResourceplanPackage.ALLOCATION__PERSON, oldPerson, person));
			}
		}
		return person;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Person basicGetPerson() {
		return person;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetPerson(Person newPerson, NotificationChain msgs) {
		Person oldPerson = person;
		person = newPerson;
		if(oldPerson != null)
			oldPerson.calcActualWorkload();
		if(newPerson != null)
			newPerson.calcActualWorkload();
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, ResourceplanPackage.ALLOCATION__PERSON, oldPerson, newPerson);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setPerson(Person newPerson) {
		if (newPerson != person) {
			NotificationChain msgs = null;
			if (person != null)
				msgs = ((InternalEObject)person).eInverseRemove(this, ResourceplanPackage.PERSON__ALLOCATIONS, Person.class, msgs);
			if (newPerson != null)
				msgs = ((InternalEObject)newPerson).eInverseAdd(this, ResourceplanPackage.PERSON__ALLOCATIONS, Person.class, msgs);
			msgs = basicSetPerson(newPerson, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ResourceplanPackage.ALLOCATION__PERSON, newPerson, newPerson));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public float getRoleWorkloadRatio() {
		return roleWorkloadRatio;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setRoleWorkloadRatio(float newRoleWorkloadRatio) {
		float oldRoleWorkloadRatio = roleWorkloadRatio;
		roleWorkloadRatio = newRoleWorkloadRatio;
		if(person != null)
			person.calcActualWorkload();
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ResourceplanPackage.ALLOCATION__ROLE_WORKLOAD_RATIO, oldRoleWorkloadRatio, roleWorkloadRatio));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case ResourceplanPackage.ALLOCATION__ROLE:
				if (role != null)
					msgs = ((InternalEObject)role).eInverseRemove(this, ResourceplanPackage.ROLE__ALLOCATIONS, Role.class, msgs);
				return basicSetRole((Role)otherEnd, msgs);
			case ResourceplanPackage.ALLOCATION__PERSON:
				if (person != null)
					msgs = ((InternalEObject)person).eInverseRemove(this, ResourceplanPackage.PERSON__ALLOCATIONS, Person.class, msgs);
				return basicSetPerson((Person)otherEnd, msgs);
		}
		return super.eInverseAdd(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case ResourceplanPackage.ALLOCATION__ROLE:
				return basicSetRole(null, msgs);
			case ResourceplanPackage.ALLOCATION__PERSON:
				return basicSetPerson(null, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case ResourceplanPackage.ALLOCATION__ROLE:
				if (resolve) return getRole();
				return basicGetRole();
			case ResourceplanPackage.ALLOCATION__PERSON:
				if (resolve) return getPerson();
				return basicGetPerson();
			case ResourceplanPackage.ALLOCATION__ROLE_WORKLOAD_RATIO:
				return getRoleWorkloadRatio();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case ResourceplanPackage.ALLOCATION__ROLE:
				setRole((Role)newValue);
				return;
			case ResourceplanPackage.ALLOCATION__PERSON:
				setPerson((Person)newValue);
				return;
			case ResourceplanPackage.ALLOCATION__ROLE_WORKLOAD_RATIO:
				setRoleWorkloadRatio((Float)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case ResourceplanPackage.ALLOCATION__ROLE:
				setRole((Role)null);
				return;
			case ResourceplanPackage.ALLOCATION__PERSON:
				setPerson((Person)null);
				return;
			case ResourceplanPackage.ALLOCATION__ROLE_WORKLOAD_RATIO:
				setRoleWorkloadRatio(ROLE_WORKLOAD_RATIO_EDEFAULT);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case ResourceplanPackage.ALLOCATION__ROLE:
				return role != null;
			case ResourceplanPackage.ALLOCATION__PERSON:
				return person != null;
			case ResourceplanPackage.ALLOCATION__ROLE_WORKLOAD_RATIO:
				return roleWorkloadRatio != ROLE_WORKLOAD_RATIO_EDEFAULT;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (roleWorkloadRatio: ");
		result.append(roleWorkloadRatio);
		result.append(')');
		return result.toString();
	}

} //AllocationImpl
